package com.liuzh.fitnessplan.fragment

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageButton
import android.widget.PopupMenu
import android.widget.TextView
import androidx.fragment.app.Fragment
import com.liuzh.fitnessplan.R
import com.liuzh.fitnessplan.activity.PlansFactorActivity
import com.liuzh.fitnessplan.database.Database
import com.liuzh.fitnessplan.database.bean.Sports
import com.liuzh.fitnessplan.databinding.FragmentMainBinding
import com.liuzh.fitnessplan.tools.RoomDatabaseUtil

class MainFragment : Fragment() {
    private lateinit var bind : FragmentMainBinding
    private val db : Database
        get() {
            return RoomDatabaseUtil.instance.getDatabase(activity!!)
        }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val mView = LayoutInflater.from(activity).inflate(R.layout.fragment_main,container,false)
        bind = FragmentMainBinding.bind(mView)
        getData()
        return mView
    }

    private fun getData() {
        if(db.sportsDao().getData().isEmpty()){
            Log.d("MainFragment","数据未正常加载")
            return
        }
        val adapter = MainFragmentAdapter(db.sportsDao().getData(),activity!!)
        bind.mainFragmentList.adapter = adapter
        adapter.notifyDataSetInvalidated()

    }



    class MainFragmentAdapter constructor(
        private var list: List<Sports>,
        private var context: Context
    ) : BaseAdapter(){
        private class ViewHolder(view : View){
            val textTitle : TextView = view.findViewById(R.id.item_title)
            val textInfo : TextView = view.findViewById(R.id.item_info)
            var buttonAdd : ImageButton = view.findViewById(R.id.item_add)
        }

        override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
            val view : View?
            val viewHolder : ViewHolder
            if(convertView == null){
                view = LayoutInflater.from(context).inflate(R.layout.item_main_fragment,parent,false)
                viewHolder = ViewHolder(view)
                view.tag = viewHolder
            }else{
                view = convertView
                viewHolder = view.tag as ViewHolder
            }
            val sports = list[position]
            viewHolder.textTitle.text = sports.title
            viewHolder.textInfo.text = "运动强度：${sports.strength}  运动耗时：${sports.sportTime}"
            viewHolder.buttonAdd.setOnClickListener {
                val intent = Intent(context,PlansFactorActivity::class.java)
                intent.putExtra("title",sports.title)
                intent.putExtra("isAdd",true)
                context.startActivity(intent)
            }

            return view as View
        }

        override fun getItem(position: Int): Any {
            return list[position]
        }

        override fun getItemId(position: Int): Long {
            return position.toLong()
        }

        override fun getCount(): Int {
            return list.size
        }

    }
}